package com.hxzy.config.security;

import com.hxzy.vo.AdminLoginVO;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 通过spring security获取当前登录用户信息
 * 功能描述
 *
 * @author tonneyyy
 */
public class SecurityUtil {

    /**
     * 得到当前用户信息
     * @return
     */
    public static AdminLoginVO getAdminLoginVO(){
        return (AdminLoginVO) getAuthentication().getPrincipal();
    }

    /**
     * 得到当前登录的UsernamePasswordAuthenticationToken
     * @return
     */
    public static Authentication getAuthentication(){

        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 得到当前用户名
     * @return
     */
    public static String getLoginName(){
        return getAdminLoginVO().getUsername();
    }
}
